﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Text;

using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class head : System.Web.UI.UserControl
    {

        DB_Member oMember = new DB_Member();
        readonly Member theMember = new Member();
       readonly DALCardType theCardType = new DALCardType();
        readonly BasePage theBase = new BasePage();
       public string MoneyMessage = string.Empty;
       // Fields
    public string headmsg = "";
    public string logo = "";
    public string shop = "0";
    public string shopname = "";
    public string shopurl = "";
    public string topic = "";
    public string totalprice = "0";
    public string typemsg = "";

    public string NewBulletin = "";
    public string BulletinId = "";
    public string MemberUserInfo = string.Empty;
    public StringBuilder sbFlash = new StringBuilder();


    public string ClassId
    {
        get {
            if (Session["classId"] == null)
            {
                return "0";
            }
            else
            {
                return Session["classId"].ToString();
            }
        }
        set { Session["classId"] = value; }
    }






    public StringBuilder sbFlashNum = new StringBuilder();
   public int FlashHeight = 220;

    // Methods
    private void Bind()
    {
        if (!string.IsNullOrEmpty(this.headmsg))
        {
            return;
        }
        this.logo = "<img src='../uploadfile/banner/" + info.info1("", "shoplogo") + "' />";
        this.shopname = info.info1("", "shopname");
        this.shopurl = info.info1("", "shopurl");
        string sql = "select classid,classlink,classname from DB_Class where parentid=0 and visible=1 and classname<>'' and (classpcs='顶部' or classpcs='顶部和底部') order by classorder";
        DataTable table = SQLAccess.GreatDs(sql).Tables[0];
        if (table.Rows.Count > 0)
        {
            foreach (DataRow row in table.Rows)
            {
                if (row["classlink"].ToString() != "")
                {
                    if (base.Request.Url.ToString().ToLower().IndexOf(row["classlink"].ToString().ToLower()).ToString() != "-1")
                    {
                        ClassId = row["classId"].ToString();
                        object obj2 = this.headmsg;
                        this.headmsg = string.Concat(new object[] { obj2, "<li class='currentPage'><a href='", row["classlink"], "'>", row["classname"], "</a></li>" });
                    }
                    else
                    {
                        object obj3 = this.headmsg;
                        this.headmsg = string.Concat(new object[] { obj3, "<li><a href='", row["classlink"], "'>", row["classname"], "</a></li>" });
                    }
                    continue;
                }
                if (base.Request.QueryString["cid"] != null)
                {
                    if (base.Request.QueryString["cid"].ToString() == row["classid"].ToString())
                    {
                        ClassId = row["classId"].ToString();
                        object obj4 = this.headmsg;
                        this.headmsg = string.Concat(new object[] { obj4, "<li class='currentPage'><a href='content.aspx?cid=", row["classid"], "'>", row["classname"], "</a></li>" });
                    }
                    else
                    {
                        object obj5 = this.headmsg;
                        this.headmsg = string.Concat(new object[] { obj5, "<li><a href='content.aspx?cid=", row["classid"], "'>", row["classname"], "</a></li>" });
                    }
                    continue;
                }
                object headmsg = this.headmsg;
                this.headmsg = string.Concat(new object[] { headmsg, "<li><a href='content.aspx?cid=", row["classid"], "'>", row["classname"], "</a></li>" });
            }
        }
        if ((re.ReadCookie("tempno") != null) ||  theBase.UserMemberId != null)
        {

            string tempId;
            if (theBase.UserMemberId != null)
            {
                tempId = theBase.UserMemberId;
                re.WriteCookie("tempno", theBase.UserMemberId, 30);
            }
            else
            {
                tempId = re.ReadCookie("tempno");
            }
           
            DataTable table2 = SQLAccess.GreatDs("select DB_Car.carid,DB_Car.pronum,DB_Car.price,DB_Car.sumprice,DB_Car.contents,DB_Shoppro.typeid,DB_Shoppro.proname,DB_Shoppro.proprice1,DB_Shoppro.propic2,DB_Shoppro.proid  from DB_Car inner join DB_Shoppro on DB_Car.productid=DB_Shoppro.proid where DB_Car.flag=0 and DB_Car.temp='" +  tempId + "'").Tables[0];
            this.shop = table2.Rows.Count.ToString();
            if (table2.Rows.Count > 0)
            {
                foreach (DataRow row2 in table2.Rows)
                {
                    this.totalprice = string.Format("{0:f}", Convert.ToSingle(row2["sumprice"]) + Convert.ToSingle(this.totalprice));
                }
            }
        }
        string str3 = "select classid,classname from DB_Productclass where parentid=0 and visible=1 order by classorder";
        DataTable table3 = SQLAccess.GreatDs(str3).Tables[0];
        if (table3.Rows.Count > 0)
        {
            foreach (DataRow row3 in table3.Rows)
            {
                string typemsg = this.typemsg;
                this.typemsg = typemsg + "<option value=" + row3["classid"].ToString() + ">" + row3["classname"].ToString() + "</option>";
            }
        }
         str3 = "select topicid,pic,picurl from DB_topic where visible=1 and isnew=1 and location=1 order by topicorder asc";
        table3 = SQLAccess.GreatDs(str3).Tables[0];
        if (table3.Rows.Count > 0)
        {
            string topic = this.topic;
            this.topic = topic + "<a href='" + table3.Rows[0]["picurl"].ToString() + "?tid=" + table3.Rows[0]["topicid"].ToString() + "' target='_blank'><img src='" + table3.Rows[0]["pic"].ToString() + "' border='0' /></a>";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        MoneyMessage = string.Empty;
        
        this.Bind();
        this.BindFlash();
        this.BindMoney();
        this.BindNewBulletin();

        if (base.Request.Url.ToString().Contains("index.aspx"))
        {
            FlashHeight = 335;
        }

    
     
    }

    protected void lbtnExit_Click(object sender, EventArgs e)
    {
         theBase.UserCardCodeList = null;

        Response.Redirect("index.aspx");
    }

    void BindMoney()
    {
        if (theBase.UserMemberId == null)
        {
            MemberUserInfo = " <a href=\"login.aspx\">【登录】</a>|<a href=\"register.aspx\">【注册】</a>";
        }
        else
        {
            MemberUserInfo = "欢迎您！" + theMember.GetModel(int.Parse(theBase.UserMemberId)).memberlogin + " <a href=\"userinfo.aspx\">【用户中心】</a>";
        }

        if (theBase.UserCardCodeList != null)
        {
          //  decimal dTotalCardPrice = 0;
            //for (int i = 0; i < theBase.UserCardCodeList.Count; i++)
            //{
            //    dTotalCardPrice += decimal.Parse(theBase.UserCardCodeList[i].UserCardPrice);
            //}

            tbCardType oCardType = theCardType.GetModel(int.Parse(theBase.UserCardCodeList[0].UserCardTypeId));
            MoneyMessage = "您有<font color='red'>" + theBase.UserCardCodeList.Count.ToString() + "</font>张:<font color='red'>" + oCardType.Name.ToString() + "</font>的礼品卡,请选取您要兑换的礼品！"
                + "<a href='exit.aspx' target='_self'>【退出】</a>";
            this.spanBulletin.Visible = false;
        }
        else
        {
            if (theBase.UserMemberId != null)
            {
                this.spanBulletin.Visible = false;
                MoneyMessage = "&nbsp;<a href='exit.aspx' target='_self'>【退出】</a>";
            }
            else
            {
                this.spanMoney.Visible = false;
            }
        }
    }


    void BindFlash()
    {
        if (!string.IsNullOrEmpty(Request.QueryString["cid"]))
        {
            ClassId = Request.QueryString["cid"];
        }
        string sFormat = " 	<li><a target=\"{3}\" href=\"{0}\"><img src=\"{1}\" onerror=\"this.src='{2}'\"  /></a></li>";
        DataTable table = SQLAccess.GreatDs("select * from DB_Flash where classid=" + ClassId + " and modelid=" + fun.banner("") + "  order by flashid asc").Tables[0];
        if (table.Rows.Count > 0)
        {
            string sFlashUrl = string.Empty;
            string sTarget = "_blank";
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (i < table.Rows.Count)
                {
                    if (table.Rows[i]["flashurl"] == null || table.Rows[i]["flashurl"].ToString() == "")
                    {
                        sFlashUrl = "#";
                        sTarget = "_self";
                    }
                    else
                    {
                        sFlashUrl = table.Rows[i]["flashurl"].ToString();
                        sTarget = "_blank";
                    }
                    sbFlash.AppendFormat(sFormat
                                         , sFlashUrl
                                         , "/uploadfile/" + table.Rows[i]["flashpic"].ToString().Replace(@"\", "/")
                                         , "/uploadfile/" + table.Rows[i]["flashpic"].ToString().Replace(@"\", "/")
                                         , sTarget
                                         );

                    sbFlashNum.AppendFormat("<li>{0}</li>", (i + 1).ToString());

                    //this.imgLink = this.imgLink + " <li><a href='" + sFlashUrl + "'><img src='" + System.Configuration.ConfigurationManager.AppSettings["DomainUrl"] + "/uploadfile/" + table.Rows[i]["flashpic"].ToString().Replace(@"\", "/") + "'/></a></li>";
                    //this.imgNumber = this.imgNumber + "<li>" + (i + 1).ToString() + "</li>";
                }
            }
        }
    }

         void BindNewBulletin()
        {
            DataSet ds = SQLAccess.GreatDs("select top 1 newsId, newscontent from db_news where typeid=4 and visible=1 and isnew=1");
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 )
            {
                BulletinId = ds.Tables[0].Rows[0]["newsId"].ToString();
                NewBulletin = ds.Tables[0].Rows[0]["newscontent"].ToString();
            }
        }


}

}